/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.domain.User;
import cc.altius.model.Disposition;
import cc.altius.model.PaymentType;
import cc.altius.service.CustomerService;
import cc.altius.service.TicketService;
import cc.altius.service.UserService;
import cc.altius.utils.DateUtils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author sagar
 */
@Controller
public class FollowupListController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private UserService userService;

    @ModelAttribute("dispositionList")
    List<Disposition> getDispositionList() {
        return this.customerService.getDispositionList();
    }

    @RequestMapping(value = "/customer/followupList.htm", method = RequestMethod.GET)
    public String showFollowupList(ModelMap modelMap, HttpServletRequest request) {
        int despId = ServletRequestUtils.getIntParameter(request, "despositionId", 0);
        List<Map<String, Object>> followupList = this.customerService.getFollowupList(despId);
        modelMap.addAttribute("followupList", followupList);
        return "/customer/followupList";
    }

    @RequestMapping(value = "/customer/followupList.htm", method = RequestMethod.POST)
    public String postFollowupList(ModelMap modelMap, HttpServletRequest request) throws ServletRequestBindingException {
        int despId = ServletRequestUtils.getIntParameter(request, "dispositionId");
        List<Map<String, Object>> followupList = this.customerService.getFollowupList(despId);
        modelMap.addAttribute("followupList", followupList);
        modelMap.addAttribute("dispositionId", despId);
        return "/customer/followupList";
    }

    @RequestMapping(value = "/customer/followupListForTicket.htm", method = RequestMethod.GET)
    public String showFollowupListForTicketGetPage(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap,
            HttpServletRequest request) {
        if (startDate.equals("0")) {
            startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }

        if (stopDate.equals("0")) {
            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }
        int despId = ServletRequestUtils.getIntParameter(request, "despositionId", 0);
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
        int paymentTypeId = ServletRequestUtils.getIntParameter(request, "paymentTypeId", 0);
        System.out.println("userId :" + userId);
        List<User> userList = this.userService.getUserList();

        List<PaymentType> paymentTypeList = this.ticketService.getPaymentTypeList();

        List<Map<String, Object>> followupList = this.ticketService.getFollowupListForTicket(despId, userId, startDate, stopDate, paymentTypeId);
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy hh:mm");
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        
        modelMap.addAttribute("curDate", curDate);
        modelMap.addAttribute("startDate", startDate);
        modelMap.addAttribute("stopDate", stopDate);
        modelMap.addAttribute("userList", userList);
        modelMap.addAttribute("followupList", followupList);
        modelMap.addAttribute("paymentTypeList", paymentTypeList);
        return "/customer/followupListForTicket";
    }

    @RequestMapping(value = "/customer/followupListForTicket.htm", method = RequestMethod.POST)
    public String postFollowupListForTicketPostPage(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap,
            HttpServletRequest request) throws ServletRequestBindingException {

        if (startDate.equals("0")) {
            startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }

        if (stopDate.equals("0")) {
            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }
        int despId = ServletRequestUtils.getIntParameter(request, "dispositionId", 0);
        int userId = ServletRequestUtils.getIntParameter(request, "userId", 0);
        int paymentTypeId = ServletRequestUtils.getIntParameter(request, "paymentTypeId", 0);
        System.out.println("userId :" + userId);
        List<Map<String, Object>> followupList = this.ticketService.getFollowupListForTicket(despId, userId, startDate, stopDate, paymentTypeId);
        List<User> userList = this.userService.getUserList();
        List<PaymentType> paymentTypeList = this.ticketService.getPaymentTypeList();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        Date curDate = DateUtils.getCurrentDateObject(DateUtils.IST);
        System.out.println("curDate :"+curDate);
        //String formatCurDate = sdf.format(curDate);
        
        modelMap.addAttribute("curDate", curDate);
        modelMap.addAttribute("paymentTypeList", paymentTypeList);

        modelMap.addAttribute("startDate", startDate);
        modelMap.addAttribute("stopDate", stopDate);
        modelMap.addAttribute("userList", userList);
        modelMap.addAttribute("followupList", followupList);
        modelMap.addAttribute("dispositionId", despId);
        modelMap.addAttribute("userId", userId);
        modelMap.addAttribute("paymentTypeId", paymentTypeId);
        return "/customer/followupListForTicket";
    }
}
